package com.mbyte.easy.support.es.helper;

import com.mbyte.easy.admin.entity.Draft;
import com.mbyte.easy.support.es.constants.EsConstants;
import com.mbyte.easy.support.es.template.ElasticsearchTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * Es稿件辅助类
 */
@Component
@Slf4j
public class EsDraftHelper {

    @Autowired
    private ElasticsearchTemplate<Draft,String> elasticsearchTemplate;

    public boolean save(Draft esQaVo) throws Exception {
        return elasticsearchTemplate.save(esQaVo);
    }

    public List<Draft> queryByText(String draftText){
        if(StringUtils.isNoneBlank(draftText)){
            QueryBuilder queryBuilder = QueryBuilders.matchQuery("draftText",draftText);
            try {
                return elasticsearchTemplate.searchWithHitScore(queryBuilder,Draft.class);
            } catch (Exception e) {
                log.error("查询异常",e);
            }
        }
        return Collections.EMPTY_LIST;
    }
    public String getDraftMaxHitScore(String draftText){
        if(StringUtils.isNoneBlank(draftText)){
            QueryBuilder queryBuilder = QueryBuilders.matchQuery("draftText",draftText);
            try {
                List<Draft> draftList = queryByText(draftText);
                if(!CollectionUtils.isEmpty(draftList)){
                    return draftList.get(0).getHitScore()+"%";
                }
                return "0%";
            } catch (Exception e) {
                log.error("查询异常",e);
            }
        }
        return null;
    }

    public boolean update(Draft esQaVo) throws Exception {
        Draft esQaVoOld = elasticsearchTemplate.getById(esQaVo.getId()+"",Draft.class);
        if(esQaVoOld != null){
            // 更新-先旧的删除缓存
            delete(esQaVo.getId());
        }
        return elasticsearchTemplate.save(esQaVo);
    }

    public boolean delete(Long id) throws Exception {
        return elasticsearchTemplate.deleteById(id+"",Draft.class);
    }

    public void delete(List<Long> idList) throws Exception {
        QueryBuilder queryBuilder = QueryBuilders.termsQuery("id",idList);
        elasticsearchTemplate.deleteByCondition(queryBuilder,Draft.class);
    }

    /**
     * 同步
     * @param draft
     * @return
     */
    public boolean sync(Draft draft){
        try {
            switch (draft.getEsSyncType()){
                case EsConstants.ES_SYNC_TYPE_ADD:
                    save(draft);
                    break;
                case EsConstants.ES_SYNC_TYPE_UPDATE:
                    update(draft);
                    break;
                case EsConstants.ES_SYNC_TYPE_DEL:
                    delete(draft.getId());
                    break;
                case EsConstants.ES_SYNC_TYPE_BATCH_DEL:
                    delete(draft.getIdList());
                    break;
                default:
                    log.error("同步es类型错误:{0}=>{1}",draft.getEsSyncType(),draft);
                    return false;
            }
        } catch (Exception e) {
            log.error("同步es推荐异常:{0}=>{1}",draft.getEsSyncType(),draft,e);
            return  false;
        }
        return true;
    }


}
